import { getAllLyric } from '@/utils/api'
import { defineStore } from 'pinia'
import { musicPlay2 } from '@/utils/playMusic'
import { Lrc } from '@/models/lyric'

export const lyric = defineStore('lyric', {
    state: () => {
        return {
            hiddenLyric: false,
            lyrics: [] as any[],
            version: -1,
            id: -1
        }
    },
    actions: {
        /* 获得歌词 */
        async getLryic() {
            // console.log('进来了')
            if (musicPlay2.audioName == '暂无音乐') return

            if (this.id == musicPlay2.id) return
            console.log('获取歌词')
            let data = await getAllLyric(musicPlay2.id)
            this.id = musicPlay2.id
            this.version = data.version
            this.lyrics = []
            console.log(data.lyric)

            // var reg = /\[(\d{2}):(\d{2}).(\d{2})\].*/g;
            var reg = /\[\d{2}:\d{2}\.\d{2,3}\].*/g;
            var songText = data.lyric.match(reg);
            console.log('@@', songText)
            if (songText) {
                for (var i = 0; i < songText.length; i++) {
                    let singleTxt = songText[i].substr(1).split(']');
                    const matches = singleTxt[0].match(/^(\d{2}):(\d{2})\.(\d{2,3})$/);
                    if (matches) {
                        const minute = parseInt(matches[1]);
                        const second = parseInt(matches[2]);
                        const millisecond = parseInt(matches[3]);
                        const timeStamp = minute * 60 * 1000 + second * 1000 + millisecond;
                        this.lyrics.push({ timeStamp, content: singleTxt[1] });
                    }
                }
            }
            console.log(this.lyrics)
        }
    },
    getters: {}
})

